﻿Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
Imports Microsoft.Office.Interop.Word
Imports System.IO
Imports System.Collections.ObjectModel
Imports Licenta3.Common.Classes

Public Class CalcululSuprafetelorClass
    Inherits AnexaClass
    Public Sub New()
        Me.ShortName = "CalcululSuprafetelor"
    End Sub
    'private Imobile as
    Private _Imobile As New ObservableCollection(Of Imobil)
    Public Property Imobile As ObservableCollection(Of Imobil)
        Get
            Return _Imobile
        End Get
        Set(value As ObservableCollection(Of Imobil))
            _Imobile = value
            OnPropertyChanged("Imobile")
        End Set
    End Property

    Public Overrides Sub toWordOrPDF(Optional format As Microsoft.Office.Interop.Word.WdSaveFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocumentDefault, Optional printOnly As Boolean = False)
        
        'Dim currentDirectory As String
        Dim Word As New Microsoft.Office.Interop.Word.Application
        Dim AnexaDoc As New Microsoft.Office.Interop.Word.Document

        Dim sablon As Object = (assemblyPath & "\WordTemplates\" & Me.ShortName & ".dotx")
        AnexaDoc = Word.Documents.Add(sablon, False, WdNewDocumentType.wdNewBlankDocument, False)
        Dim currentDirectory = SetariClass._LucruPath & "\" & Me.Autorizat.Nume & "\" & Me.OCPI.denumire & "\" & Me.Imobil.UAT.Denumire & "\" & Me.proprietar.Nume
        If IO.Directory.Exists(currentDirectory) = False Then
            IO.Directory.CreateDirectory(currentDirectory)
        End If

        FillText("OCPI", AnexaDoc, Me.OCPI.denumire)
        FillText("BCPI", AnexaDoc, Me.BCPI.denumire)
        'FillText("SIRSUP",AnexaDoc, Me.Imobil.UAT.sirsup) 'de implementat
        Dim tabelSuprafata As Microsoft.Office.Interop.Word.Table
        Dim bmMain As Bookmark = AnexaDoc.Bookmarks("Content")
        'Dim tbRanges As New List(Of Range)
        Dim tbRange As Range = bmMain.Range
        'tbRange.InsertParagraphAfter()
        Dim bm As Bookmark = bmMain
        tbRange.Select()
        For i = 0 To Imobile.Count - 1
            tabelSuprafata = AnexaDoc.Tables.Add(Word.Selection.Range, Imobile(i).Polyline.Geometry.NumberOfVertices + 1, 3)
            With tabelSuprafata
                .Cell(1, 1).Range.Text = "Nr."
                .Cell(1, 2).Range.Text = "Nord"
                .Cell(1, 3).Range.Text = "Est"
                .ApplyStyleHeadingRows = True
                For r = 2 To .Rows.Count
                    .Cell(r, 1).Range.Text = (r - 1).ToString
                    .Cell(r, 2).Range.Text = Math.Round(Me.Imobile(i).Polyline.Geometry.GetPoint2dAt(r - 2).Y, 3).ToString
                    .Cell(r, 3).Range.Text = Math.Round(Me.Imobile(i).Polyline.Geometry.GetPoint2dAt(r - 2).X, 3).ToString
                Next
            End With
            tabelSuprafata.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle
            tabelSuprafata.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle
            'tabelSuprafata.Range.Collapse(Direction:=WdCollapseDirection.wdCollapseEnd)
            'tabelSuprafata.Range.InsertParagraphAfter()
            'tbRange.InsertParagraphAfter()
            'tabelSuprafata.Range.Collapse(Direction:=WdCollapseDirection.wdCollapseEnd)
            'tabelSuprafata.Range.Select()
            'tbRange.InsertParagraphAfter()
            'tabelSuprafata.Range.Collapse(Direction:=WdCollapseDirection.wdCollapseEnd)
            tabelSuprafata.Select()
            'tbRange.Select()
            Word.Selection.Collapse(WdCollapseDirection.wdCollapseEnd)

            'Word.Selection.EndKey(WdUnits.wdStory, Nothing)
            'tbRange.Collapse(Direction:=WdCollapseDirection.wdCollapseEnd)
            'tbRange.InsertParagraphAfter()
            'tbRange.Collapse(Direction:=WdCollapseDirection.wdCollapseEnd)
            'tbRange.Select()
            'tbRange.EndOf(WdUnits.wdTable, WdMovementType.wdMove)
            'tbRange.EndOf(WdUnits.wdParagraph, WdMovementType.wdMove)

            'tbRange = tbRange.Next(WdUnits.wdParagraph)

        Next
        FillText("NumeMandatar", AnexaDoc, Autorizat.Nume)


        'Dim tabelImobile As Microsoft.Office.Interop.Word.Table
        'tabelImobile = AnexaDoc.Tables.Add(AnexaDoc.Bookmarks("TabelImobile").Range, Imobile.Count + 1, 4)
        'With tabelImobile
        '    .Cell(1, 1).Range.Text = "Adresa"
        '    .Cell(1, 2).Range.Text = "Nr. CF"
        '    .Cell(1, 3).Range.Text = "Nr. Cadastral"
        '    .Cell(1, 4).Range.Text = "UAT"
        '    .ApplyStyleHeadingRows = True
        '    For r = 2 To .Rows.Count
        '        .Cell(r, 1).Range.Text = Me.Imobile(r - 2).Adresa.adresaIntreaga
        '        .Cell(r, 2).Range.Text = Me.Imobile(r - 2).NrCF
        '        .Cell(r, 3).Range.Text = Me.Imobile(r - 2).NrCadastral
        '        .Cell(r, 4).Range.Text = Me.Imobile(r - 2).UAT
        '    Next
        'End With
        'tabelImobile.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle
        'tabelImobile.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle

        Dim ext As String = ".docx"
        If format = WdSaveFormat.wdFormatPDF Then ext = ".pdf"
        If printOnly = True Then
            Try
                AnexaDoc.Activate()
                If File.Exists(currentDirectory & "\" & Me.ShortName & ".docx") = False Then
                    AnexaDoc.SaveAs2(currentDirectory & "\" & Me.ShortName & ".docx")
                Else
                    File.Delete(currentDirectory & "\" & Me.ShortName & ".docx")
                    AnexaDoc.SaveAs2(currentDirectory & "\" & Me.ShortName & ".docx")
                End If
                Word.PrintOut(FileName:=currentDirectory & "\" & Me.ShortName & ".docx")

            Catch ex As Exception
                MsgBox(ex.Message + vbCrLf + ex.Source, MsgBoxStyle.Information)
            End Try
        Else
            Try
                If File.Exists(currentDirectory & "\" & Me.ShortName & "" & ext) = False Then
                    AnexaDoc.SaveAs2(currentDirectory & "\" & Me.ShortName & "" & ext, format)
                Else
                    If MsgBox("Fisierul exista! Doriti sa-l suprascrieti?", MsgBoxStyle.YesNo, "Sunteti sigur?") = MsgBoxResult.Yes Then
                        File.Delete(currentDirectory & "\" & Me.ShortName & ext)
                        AnexaDoc.SaveAs2(currentDirectory & "\" & Me.ShortName & ext, format)
                    Else
                        MsgBox("Niciun fisier nu a fost modificat", MsgBoxStyle.OkOnly)
                    End If
                End If
            Catch ex As Exception
                MsgBox(ex.Message + vbCrLf + ex.Source + vbCrLf + ex.StackTrace, vbOKOnly)
            End Try
        End If

        AnexaDoc.Close(WdSaveOptions.wdDoNotSaveChanges)
        AnexaDoc = Nothing
        'AnexaDoc.SaveAs2(currentDirectory & "\ANEXA1.docx")
        'Word.Visible = True
        Word.Quit(WdSaveOptions.wdDoNotSaveChanges)
        Word = Nothing
    End Sub


End Class
